package com.leo.lime.music.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatSeekBar;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;

import com.leo.lime.music.Main;
import com.leo.lime.music.R;
import com.leo.lime.music.adapter.ContentViewPagerAdapter;
import com.leo.lime.music.content.me.fragment.MeFragment;
import com.leo.lime.music.controller.MusicController;
import com.leo.lime.music.controller.SharedPrefController;

import com.leo.lime.music.content.listener.fragment.ListenerFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by android on 18-8-9.
 */
/**
 * Created For Main Content View
 */
public class ContentFragment extends Fragment implements MusicController.Listener {
    private static final String TAG = "ContentFragment";

    private ViewPager mViewPager;
    private ImageView mMiniViewIv;
    private AppCompatSeekBar mMiniViewSb;
    private AppCompatTextView mMiniViewMusicInfoTv;
    private ImageView mMiniViewPlayPauseIv;
    private ImageView mMiniViewNextIv;
    private ImageView mMiniViewListIv;
    private View mMiniView;

    private MusicController mController;

    private PagerChangedListener mPagerChangedListener;

    private View.OnClickListener mContentViewClickListener = new View.OnClickListener() {
        public void onClick(View view) {

        }
    };

    private ViewPager.OnPageChangeListener mContentViewPagerChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            mPagerChangedListener.onPagerChangedListener(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mPagerChangedListener = (Main) getActivity();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mController = MusicController.getInstance(this.getActivity());
        mController.addListener(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main_content_view, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mViewPager = view.findViewById(R.id.content_viewpager);
        mMiniViewIv = view.findViewById(R.id.content_mini_music_icon);
        mMiniViewSb = view.findViewById(R.id.mini_seekbar);
        mMiniViewMusicInfoTv = view.findViewById(R.id.content_mini_view_musicinfo);
        mMiniViewPlayPauseIv = view.findViewById(R.id.mini_music_play_pause);
        mMiniViewNextIv = view.findViewById(R.id.mini_music_next);
        mMiniViewListIv = view.findViewById(R.id.mini_music_list);
        mMiniView = view.findViewById(R.id.content_mini_view);

        mMiniView.setOnClickListener(mContentViewClickListener);
        mMiniViewPlayPauseIv.setOnClickListener(mContentViewClickListener);
        mMiniViewNextIv.setOnClickListener(mContentViewClickListener);
        mMiniViewListIv.setOnClickListener(mContentViewClickListener);

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new MeFragment());
        fragments.add(new ListenerFragment());
        fragments.add(new LookFragment());
        fragments.add(new SingFragment());
        mViewPager.setAdapter(new ContentViewPagerAdapter(getActivity().getSupportFragmentManager(), fragments));
        mViewPager.setCurrentItem(SharedPrefController.getInstance(getActivity()).getContentPagerIndex());
        mViewPager.addOnPageChangeListener(mContentViewPagerChangeListener);
    }

    public void updateUi() {

    }

    public interface PagerChangedListener {
        void onPagerChangedListener(int index);
    }
}
